<?php
namespace App\Http\Controllers;

use Illuminate\Http\JsonResponse;
use Illuminate\Http\Response;

class BaseController extends Controller
{
    /**
     * 成功响应格式
     *
     * @param mixed $data    返回数据
     * @param string $message 提示信息
     * @param int $code      HTTP状态码
     * @return JsonResponse
     */
    protected function success( mixed $data = [], string $message = 'Success'): JsonResponse
    {
        return response()->json([
            'success' => true,
            'code'    => 0,
            'message' => $message,
            'data'    => $data
        ], 200);
    }

    /**
     * 失败响应格式
     *
     * @param string $message 错误信息
     * @param int $code       HTTP状态码
     * @param mixed $errors   详细错误（调试用）
     * @return JsonResponse
     */
    protected function error(string $message = 'Error', int $code = Response::HTTP_BAD_REQUEST, mixed $errors = []): JsonResponse
    {
        $response = [
            'success' => false,
            'code'    => $code,
            'message' => $message,
        ];

        // 生产环境隐藏错误细节
        if (config('app.debug')) {
            $response['errors'] = $errors;
        }

        return response()->json($response, 200);
    }
}

